<?php  
namespace Admin\Controller;

use Think\Controller;

class adController extends AdminController{
	public function index(){
		$ad = M("ad");
		$Page = new\Think\Page($count,5);
		$show = $Page->show();
		$list = $ad ->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	public function insert(){
		$ad = D("ad");
		$upload = new\Think\Upload();
		$upload->maxSize = 3145728;
		$upload->exts = array('jpg','gif','png','jpeg');
		$upload->rootPath = './Public'; 
		$upload->savePath = "/upload/image/";
		$upload->subName = "";
		$info = $upload->upload();
	 	$_POST['logo']=$info['photo']['savename'];

	 	if($ad->create()){
	 		
	 		// $result=$ad->add();
	 	    if(empty($_POST['adname'])){
	 	    	$this -> error('广告名不能为空!');
	 	    }else if(empty($_POST['urlname'])){
	 	    	$this -> error('链接url不能为空');
	 	    }else if(empty($_POST['logo'])){
	 	    	$this -> error('logo不能为空!');
	 	    }
	 		dump($result = $ad ->fetchSql(true)->add());
	 		exit;
	 			if($result){
	 					$this->success('数据添加成功',U('ad/index'));
	 						}else{$this->error('数据添加失败',U('ad/index'));}
	 					}else{
	 		$this->error($ad->getError());}
	 	}

	public function edit(){
			$ad = M('ad');
			$vo = $ad->where(array('id'=>array('eq',$_GET['id'])))->find();
			$this->assign('vo',$vo);
			$this->display();
		}
	
	public function save(){
		$ad = M('ad');
			$upload = new\Think\Upload();
			$upload->maxSize = 3145728;
			$upload->exts = array('jpg','gif','png','jpeg');
			$upload->rootPath = './Public'; 
			$upload->savePath = "/upload/image/";
			$upload->subName = "";
			$info = $upload->upload();
	 		$_POST['logo']=$info['photo']['savename'];
		if($ad->create()){	
			$result =  $ad->save();
			if($result){
				$this->success('操作成功!',U('ad/index'));
			}else{
				$this->error('写入错误!');
			}
		}else{
			$this->error($ad->getError());
		}
	}

	public function del(){
		$ad = M('ad');
		$result=$ad -> where(array('id'=>array('eq',$_GET['id'])))->delete();
			if($result){
					$this->success("删除成功！",U('ad/index'));
			}else{
				$this->error("删除失败");
			}
	}
	 
}